import CommonAPI, { AllMaterialCategoryForm } from "@/api/system/common";
import { arrayToTree } from "@/utils/handledata";
export function useCategory(defaultCategoryParams = {}) {
  const materialCategoryList = ref([]);

  const getMaterialCategoryList = computed(() => {
    const map = new Map();
    const tree = [];

    // 先把所有节点存入 Map
    materialCategoryList.value.forEach((item) => map.set(item.code, { ...item, children: [] }));

    // 遍历数据，构建树结构
    materialCategoryList.value.forEach((item) => {
      if (item.parentCode === "0") {
        tree.push(map.get(item.code));
      } else {
        map.get(item.parentCode)?.children.push(map.get(item.code));
      }
    });

    return tree;
  });
  // 获取几级内的数据
  function getSpecialLevelList(level: number) {
    let data = [...materialCategoryList.value];
    return arrayToTree(data, level);
  }
  async function getMaterialCategory() {
    // try {
    //   const params: AllMaterialCategoryForm = {
    //     isLeaf: false,
    //     enable: true,
    //     ...defaultCategoryParams,
    //   };
    //   const data = await CommonAPI.getAllMaterialCategory(params);
    //   materialCategoryList.value = data.data.map((item: any) => {
    //     return {
    //       ...item,
    //       disabled: !item.selfOwn,
    //     };
    //   });
    // } catch (e) {
    //   console.log(e);
    // }
  }

  return {
    // materialCategoryList,
    getMaterialCategoryList,
    getMaterialCategory,
    getSpecialLevelList,
  };
}
